<%@include file="/cs/common/charset.jsp" %>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<!-- import JAVA components -->
<%@ page import="org.apache.struts.util.RequestUtils" %>
<%@ page import="com.broadvision.ssp.client.*" %>
<%@ page import="com.broadvision.data.client.*" %>
<%@ page import="com.broadvision.portal.services.PortalVisitor" %>
<%@ page import="com.broadvision.web.servlet.SessionVisitor" %>
<%@ page import="com.broadvision.portal.services.Program" %>
<%@ page import="com.broadvision.data.client.DataManager" %>
<%@ page import="com.broadvision.data.client.Content" %>
<%@ page import="com.broadvision.data.common.ContentUID" %>
<%@ page import="com.broadvision.data.common.DataConstants" %>
<%@ page import="com.broadvision.system.accesscontrol.client.AccessManager" %>
<%@ page import="com.broadvision.visitor.client.Visitor" %>

<%
  String processContentURL = request.getParameter("_process_content");

  // checks if this is the first time in the request coming to this page
  //if (processContentURL == null) {

    // Get the visitorId and the serviceId
    SessionVisitor sessionVisitor = SessionVisitor.getInstance(request.getSession());
    Visitor visitor = sessionVisitor.getVisitor();
    int _serviceId = sessionVisitor.getServiceId();

    // Get Content type Id from request
    String contentTypeIdStr = request.getParameter("contentTypeId");

    // Get the SSP OID;
    String sspOidStr = request.getParameter("processOID");

    if (  contentTypeIdStr == null || contentTypeIdStr.length() == 0
       || sspOidStr == null || sspOidStr.length() == 0 ) {
      %> Error: ContentTypeId and processOID are expected but were not passed in <% 
      return;
    }

    // Get the Content OID
	String contentOIDStr = request.getParameter("contentOID");

    // prepare initValues which include 
    // The prefix "BV_II_" of init value name is required for 
    // passing init name/value pairs to /startWebflow.do

    String initValues = "&BV_II_ContentTypeId=" + contentTypeIdStr;

    // if "edit" action, check if visitor has permission to edit content item
    String CONTENTADMIN  = "ContentAdmin";
    String PERMISSION_EDIT = "EditContent";
    if ( contentOIDStr!=null && contentOIDStr.length() != 0 ) {
      try {
        long contentOID = Long.parseLong(contentOIDStr);
        int contentTypeId = Integer.parseInt(contentTypeIdStr);

        DataManager dataMgr = new DataManager();
        AccessManager accessMgr = AccessManager.instance();

        boolean bContentAdmin = accessMgr.isUserInRole(visitor, CONTENTADMIN);
        boolean bPermitted = false;

        // if no content admin role, need ACL check
        if (!bContentAdmin) {
          ContentUID contUid = new ContentUID(contentTypeId, _serviceId, contentOID);
          Content cnt = dataMgr.getContent(contUid);
          bPermitted = accessMgr.isPermitted(visitor, cnt, PERMISSION_EDIT);
        }

        // no permission
        if (!bPermitted && !bContentAdmin) {
          %> Error: You do not permission to edit this content item <%
          return;
        }
      }
      catch (Exception ex) {
          %> Error in processing your permissions on the content item <%
          return;
      }
    
      // this is the case for editing an existing content: Init values
      initValues += "&BV_II_ContentOID=" + contentOIDStr;

    }
    else {
      // get the category OID String
      String catOIDStr = request.getParameter("categoryOID");
      initValues += "&BV_II_CategoryOID=" + catOIDStr;
    }
	
    String thisPage = request.getServletPath();
    String url = "/startWebflow.do?reqParam=_process_content&wantCtx=false"
                + "&container=" + "/ep/contentPublishAction.do"
                + "&exitURL=/ep/closeWindowAlwaysRefresh.do"
                + "&ssp_process_oid=" + sspOidStr
                + initValues;

    String sspURL = response.encodeRedirectURL(url.toString());

	// now start the process by forwarding to sspURL, 
	// it will eventually come back to this container page
	// once the process is started.
    pageContext.forward(sspURL);

//  }
//  else {
    // it's coming to this container page when a publishing process 
    // has already started. We include the current process page within
    // this container page.

  %>


<%
//  }
%>
